/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.main;

import com.core.utils.DateUtil;
import com.core.log.Debug;
import com.server.database.DataEngine;
import com.server.database.SmsInfo;
import com.server.database.UserInfo;
import com.server.database.UserMessage;
import com.server.game.GameManager;
import com.server.game.GamePlayer;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ChargeMoneySmS implements Runnable {

    private boolean isRunning;
    private GameManager manager;
    private DataEngine dataEngine = DataEngine.getInstance();
    private long startTime = 0;
    private static final int DELAY_TIME = 3000;

    public ChargeMoneySmS(GameManager gameManayer) {
        this.manager = gameManayer;
    }

    public void start() {
        isRunning = true;
        startTime = System.currentTimeMillis();
        new Thread(this).start();
    }

    private void printMemory() {
//        System.out.println("Max memory    :" + (Runtime.getRuntime().maxMemory() >> 20));
        System.out.println("--------------------------------------------------------------");
        System.out.println("Free memory   :" + (Runtime.getRuntime().freeMemory() >> 20));
//        System.out.println("Total memmory :" + (Runtime.getRuntime().totalMemory() >> 20));
        System.out.println("Use memmory :" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20));
    }

    @Override
    public void run() {
        while (isRunning) {
            try {
                Thread.sleep(DELAY_TIME);
            } catch (Exception ex) {
            }
            if (System.currentTimeMillis() - startTime > DELAY_TIME) {
                startTime = System.currentTimeMillis();
//                printMemory();
            }
            List<SmsInfo> list = dataEngine.getSmsQueue();
            dataEngine.logTransaction();
            if (!list.isEmpty()) {
                while (!list.isEmpty()) {
                    SmsInfo info = list.get(0);
                    GamePlayer player = manager.getPlayer(info.getSmsInfo());
                    if (player != null) {
                        // neu truong hop dang online
                        UserInfo userInfo = player.getUserInfo();
                        if (userInfo.getActivated() == 0 && info.getServiceCode().equals("8085")) {
                            dataEngine.activateAcc(info.getSmsInfo(), userInfo.getCompanyId());
                            manager.sendMessage(player.parent, "Thông báo", info.getSmsInfo(), UserMessage.MESSAGE_NORMAL,
                                    "Bạn vừa kích hoạt tài khoản thành công . Xin cảm ơn");
                        } else {
                            player.updateMoneyDelta(getGold(info.getServiceCode()));
                            manager.sendMessage(player.parent, "Thông báo", info.getSmsInfo(), UserMessage.MESSAGE_NORMAL,
                                    "Bạn vừa nạp " + getGold(info.getServiceCode()) + " xu vào tài khoản . Xin cảm ơn");
                        }
                    } else {
                        UserInfo userInfo = dataEngine.getUserInfo(info.getSmsInfo());
                        if (userInfo != null) {
                            if (userInfo.getActivated() == 0 && info.getServiceCode().equals("8085")) {
                                activateAccount(info);
                            } else {
                                dataEngine.updateMoney(userInfo.getMoney() + getGold(info.getServiceCode()), info.getSmsInfo());
                                dataEngine.insertNewMessenger("Thông báo", info.getSmsInfo(), "Bạn vừa nạp " + getGold(info.getServiceCode()) + "xu vào tài khoản . Xin cảm ơn",
                                        DateUtil.newTimestamp(), UserMessage.MESSAGE_NORMAL);
                            }
                        }
                    }
                    list.remove(0);
                }
            }
        }
    }

    private void activateAccount(SmsInfo info) {
        ClientManager client = manager.getWaitActiveClient(info.getSmsInfo());
        if (client != null) {
            dataEngine.activateAcc(info.getSmsInfo(), client.getCompanyId());
            client.sendMessage("GRANTED|");
            dataEngine.insertNewMessenger("Thông báo", info.getSmsInfo(), "Bạn vừa kích hoạt tài khoản thành công . Xin cảm ơn",
                    DateUtil.newTimestamp(), UserMessage.MESSAGE_NORMAL);
        }
//        manager.activateClient(info.getSmsInfo());
    }

    private int getGold(String serviceNumber) {
        return getCostNumber(serviceNumber) * 10;
    }

    public int getCostNumber(String number) {
        int cost = 500;
        if (number.charAt(1) == '0') {
            cost = 500;
        } else if (number.charAt(1) == '1') {
            cost = 1000;
        } else if (number.charAt(1) == '2') {
            cost = 2000;
        } else if (number.charAt(1) == '3') {
            cost = 3000;
        } else if (number.charAt(1) == '4') {
            cost = 4000;
        } else if (number.charAt(1) == '5') {
            cost = 5000;
        } else if (number.charAt(1) == '6') {
            cost = 10000;
        } else if (number.charAt(1) == '7') {
            cost = 15000;
        }
        return cost;
    }
}
